<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="卖家账户" prop="sellerId">
        <el-select
          v-model="queryParams.sellerId"
          placeholder="请选择"
          clearable
          filterable
          @change="getEvaluationBalance"
        >
          <!-- <el-option
            v-for="dict in storeList"
            :key="dict.value"
            :label="dict.lable"
            :value="dict.value"
          /> -->
          <el-option-group
            v-for="group in storeList"
            :key="group.label"
            :label="group.label">
            <el-option
              v-for="item in group.options"
              :key="item.userId"
              :label="item.name"
              :value="item.userId">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="流水类型" prop="flowType">
        <el-select v-model="queryParams.flowType" placeholder="请选择流水类型" clearable>
          <el-option
            v-for="dict in dict.type.erp_flow_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in dict.type.erp_pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <div v-if="evaluationBalance">测评金额：{{ evaluationBalance }}</div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellerStoreAccountLogList" border stripe
              @selection-change="handleSelectionChange">
      <el-table-column label="时间" align="center" prop="addTime"/>
      <el-table-column label="流水类型" align="center" prop="flowType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_flow_type" :value="scope.row.flowType"/>
        </template>
      </el-table-column>
      <el-table-column label="费用类型" align="center" prop="expenseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_expense_type" :value="scope.row.expenseType"/>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="用户" align="center" prop="sellerId">
        <template slot-scope="scope">
          <dict-tag :options="userList" :value="scope.row.sellerId"/>
        </template>
      </el-table-column>
      <el-table-column label="流水" align="center" prop="money"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手工账日志记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="卖家账号" prop="sellerId">
          <el-select v-model="form.sellerId" filterable placeholder="请选择卖家账号">
            <el-option-group
              v-for="(group,index) in storeList"
              :key="index"
              :label="group.label">
              <el-option
                v-for="(item,index1) in group.options"
                :key="index1"
                :label="item.name"
                :value="item.userId">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="流水类型" prop="flowType">
          <el-select v-model="form.flowType" placeholder="请选择流水类型">
            <el-option
              v-for="dict in dict.type.erp_flow_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用类型" prop="expenseType">
          <el-select disabled v-model="form.expenseType" placeholder="请选择费用类型">
            <el-option
              v-for="dict in dict.type.erp_expense_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付方式">
            <el-option
              v-for="dict in dict.type.erp_pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流水" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSellerList
} from "@/api/seller/sellerStoreAccountLog";
import {queryEvaluationLog, updateEvaluationAmount} from "@/api/evaluation/evaluation";
import {getWalletExpend} from "@/api/product/online/list";

export default {
  dicts: ['erp_flow_type', 'erp_pay_type', 'erp_expense_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手工账日志记录表格数据
      sellerStoreAccountLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowType: undefined,
        expenseType: undefined,
        payType: undefined,
        sellerId: undefined,
        money: undefined,
        orderNo: undefined,
      },
      // 表单参数
      form: {expenseType: "测评费用",},
      storeList: [],// 卖家列表
      userList: [],// 卖家列表
      // 表单校验
      rules: {
        flowType: [
          {required: true, message: "流水类型不能为空", trigger: "change"}
        ],
        expenseType: [
          {required: true, message: "费用类型不能为空", trigger: "change"}
        ],
        payType: [
          {required: true, message: "支付方式不能为空", trigger: "change"}
        ],
        sellerId: [
          {required: true, message: "卖家id不能为空", trigger: "change"}
        ],
        money: [
          {required: true, message: "流水不能为空", trigger: "blur"}
        ],
      },
      evaluationBalance: ""
    };
  },
  created() {
    this.getAllSellerList();
    this.getList();
  },
  methods: {
    // 获取测评余额
    getEvaluationBalance(e) {
      console.log('eee', e)
      getWalletExpend({
        userId: e,
      }).then(res => {
        console.log('reess', res)
        this.evaluationBalance = res.data
      })
    },
    // 获取店铺账号选择列表
    getAllSellerList() {
      this.loading = true;
      getSellerList().then(res => {
        const newArr = []
        this.userList = []
        for (let key in res.data) {
          var list = res.data[key];
          newArr.push({label: key, options: list})
          for (let obj in list) {
            this.userList.push({label: list[obj].name, value: list[obj].userId})
          }
        }
        console.log(this.userList)
        this.storeList = newArr
        this.loading = false;
      });
    },
    /** 查询手工账日志记录列表 */
    getList() {
      this.loading = true;
      queryEvaluationLog(this.queryParams).then(res => {
        this.sellerStoreAccountLogList = res.rows
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        flowType: undefined,
        expenseType: "测评费用",
        payType: undefined,
        userId: undefined,
        money: undefined,
        orderNo: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增测评金额";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.expenseType = 14
          updateEvaluationAmount(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('seller/sellerStoreAccountLog/export', {
        ...this.queryParams
      }, `sellerStoreAccountLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
